package com.lt.app;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.lt.base.BaseApplication;
import com.lt.LibConstant;

/**
 * 资源辅助类
 *
 * @author luotian
 */
public class ResHelper {
    private static ResHelper instance;

    public static final String LAYTOUT = "layout";
    public static final String DRAWABLE = "drawable";
    public static final String MIPMAP = "mipmap";
    public static final String MENU = "menu";
    public static final String RAW = "raw";
    public static final String ANIM = "anim";
    public static final String STRING = "string";
    public static final String STYLE = "style";
    public static final String STYLEABLE = "styleable";
    public static final String INTEGER = "integer";
    public static final String ID = "id";
    public static final String DIMEN = "dimen";
    public static final String COLOR = "color";
    public static final String BOOL = "bool";
    public static final String ATTR = "attr";

    private ResHelper() {
    }

    public static ResHelper getInstance() {
        if (instance == null) {
            synchronized (ResHelper.class) {
                instance = new ResHelper();
            }
        }
        return instance;
    }


    @NonNull
    public static String getString(int rid) {
        try {
            return BaseApplication.getContext().getResources().getString(rid);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return String.valueOf(rid);
    }

    /**
     * String.format("money:￥%.2f",1.00);
     */
    public static String getString(@StringRes int rid, Object... o) {
        try {
            return BaseApplication.getContext().getResources().getString(rid, o);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return LibConstant.unknown;
    }

    public Drawable getDrawable(@DrawableRes int resId) {
        try {
            return ResourcesCompat.getDrawable(BaseApplication.getContext().getResources(), resId, null);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getColor(@ColorRes int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ContextCompat.getColor(BaseApplication.getContext(), id);
        } else {
            return BaseApplication.getContext().getResources().getColor(id);
        }
    }

    public int getColor(String color) {
        return Color.parseColor(color);
    }

    public String getColorStrByRes(@ColorRes int id) {
        return String.format("#%06X", 0xFFFFFF & getColor(id));
    }


    /**
     * getResourceId(getApplicationContext(),"activity_main",ResourcesUtil.LAYTOUT);
     *
     * @param name 资源名
     * @param type 资源类型
     * @return 资源ID
     */
    public int getResourceId(String name, String type) {
        Resources resources;
        try {
            resources = BaseApplication.getContext().getResources();
            return resources.getIdentifier(name, type, BaseApplication.getContext().getPackageName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int getResourceId(String resName, String type, int defaultId) {
        int id = getResourceId(resName, type);
        return id == 0 ? defaultId : id;
    }

    public int getDimensPx(@DimenRes int id) {
        try {
            return BaseApplication.getContext().getResources().getDimensionPixelSize(id);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
